package com.synjones.handsetS8.ild.utils;

import android.content.Context;
import android.graphics.Bitmap;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Chris Chan
 * Create on: 2021/7/21}10:56
 * For:
 * Explain:
 */
public class FileUtil {
    /**
     * 保存图片
     *
     * @param bitmap
     * @param path
     * @throws IOException
     */
    public static String saveImage(Bitmap bitmap, String path) throws IOException {
        String imagePath = "";
        String imageName = "img";
        File filePath = new File(path);
        if (!filePath.exists()) {
            filePath.mkdir();
        }
        int i = 0;
        while (filePath.exists()) {
            imagePath = path + "/" + imageName + (i++) + ".jpg";
            filePath = new File(imagePath);
        }
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(filePath));
        // 压缩位图到指定的OutputStream
        bitmap.compress(Bitmap.CompressFormat.JPEG, 0, bos);
        // 刷新此缓冲区的输出流
        bos.flush();
        // 关闭此输出流并释放与此流有关的所有系统资源
        bos.close();
        return imagePath;
    }

    public static File getSaveFile(Context context) {
        File file = new File(context.getFilesDir(), "pic.jpg");
        return file;
    }

}
